<template>
  <div>
    <el-dialog
      :title="$t('LineManagementLang.Timely')"
      :visible.sync="Visible"
      width="50%"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <div class="midContant clearfix">
        <MyTable
        :tableColumn="tableColumn"
        :tableData="tableData"
        :isOrder="false"
        @sendTabClick="setTabClick"
        >
          <template v-slot:content="scope">
              <span v-if="scope.row.index === tabClickIndex && tabClickLabel === scope.config.label&&typeName!=='details'">
                <el-input
                  v-fo 
                  v-if="scope.config.templateType=='Input'"
                  @blur="inputBlur(scope.row,scope.config.prop)"
                  v-model="scope.row[scope.config.prop]"
                ></el-input>
                <span v-else>{{ scope.row[scope.config.prop] }}</span>
              </span>
              <span v-else>{{ scope.row[scope.config.prop] }}</span>
            </template>
        </MyTable>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button  type="primary" @click="Previous_step" size="small">{{$t('LineManagementLang.Previous_step')}}</el-button
        >
        <el-button @click="next_step" size="small"
          >{{$t('LineManagementLang.next_step')}}</el-button
        >
      </span>
      
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
  },
  data() {
    return {
      Visible: false,
      id:'',
      tableColumn:[
        {
          label: "",
          prop: "table_code",
          width: 40,
        },
        {
          checked: true,
          label: this.$t('LineManagementLang.destination'),
          prop: "target_local_name",
        },
        {
          checked: true,
          label: this.$t('LineManagementLang.ageing_zh'),
          prop: "duration",
          width: 140,
          templateType:'Input',
        },
        {
          checked: true,
          label: this.$t('LineManagementLang.ageing_en'),
          prop: "duration_en",
          width: 140,
          templateType:'Input',
        },
        {
          checked: true,
          label: this.$t('LineManagementLang.ageing_ru'),
          prop: "duration_ru",
          width: 140,
          templateType:'Input',
        },

      ],
      tableData:[],
      tabClickIndex:null,
      tabClickLabel:null,
      typeName:'',//判断是否为详情页跳转过来
      confirmTrue:true,//确定按钮保护
    };
  },
  created() {
    
  },
  mounted() {
  },
  deactivated(){
    
  },
  watch: {
  },
  methods: {
    init(data,name) {
      this.typeName=name
      this.Visible = true;
      this.id=data.id
      this.getData();
    },
    //获取单条
    getData() {
      this.$get("lineTargetDuration", { id: this.id}).then((res) => {
        this.tableData = res.data;
      });
    },
    setTabClick({index,label}){
      this.tabClickIndex=index
      this.tabClickLabel=label
    },
    // 失去焦点初始化
    inputBlur (row,prop) {
      this.tabClickIndex = null
      this.tabClickLabel = null   
    },
    //取消修改
    cancel() {
      this.tableData=[]
      this.Visible = false;
      this.tabClickIndex = null
      this.tabClickLabel = null 
    },
    next_step(){
      if(this.typeName==='details'){
        this.$emit("next_step",this.typeName);
        this.cancel()
        return
      }
      //是否有空值必选项标识
      let type = true;
      let isComplete=true
      this.tableData.forEach(el=>{
        if(!el.duration||!el.duration_en||!el.duration_ru){
          type=false
          isComplete=false
        }
      })
      if(!isComplete){
        this.$message.warning(this.$t('LineManagementLang.message1'));
      }
      if (type && this.confirmTrue) {
        this.confirmTrue=false
        window.setTimeout(()=>{
          this.confirmTrue=true
        },1000);
        let params = new FormData();
        params.append("id", this.id);
        params.append("data", JSON.stringify(this.tableData));
        this.$postForm("saveLineTargetDuration", params).then((res) => {
          this.confirmTrue = true;
          if(res.success){
            this.$message.success(res.msg);
            this.$emit("next_step");
            this.cancel()
          }
        });
      }
    },
    Previous_step(){
      this.$emit('Previous_step',this.typeName)
      this.cancel()
    },
  },
};
</script>

<style scoped lang="less" >
  .midContant {
    border: 0;
    width: 100%;
  }
  /deep/ .el-dialog__wrapper{
    .el-dialog{
      min-width: 500px;
    }
  }
</style>